import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:yx_flutter/YXLocalizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yx_flutter/common/YXToastUtil.dart';
import 'YXUpdatePasswordProvide.dart';
import 'package:yx_flutter/common/YXPassWordTextField.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:yx_flutter/base/YXBaseView.dart';

// 更新密码页面
class YXUpdatePasswordView extends YXBaseView {
  Map params;

  YXUpdatePasswordView({this.params}) : super(params);

  @override
  Widget build(BuildContext context) {
    // 返回按钮点击方法
    pageBack() {
      print('点击了返回按钮');
      // FocusScope.of(context).requestFocus(FocusScopeNode());
      // MethodChannel("YXUpdatePaswordVCMethod").invokeMethod('pageBack');
      FlutterBoost.singleton
          .close('flutter://closePage', result: {'params': this.params});
    }

    // 初始化尺寸
    final size = MediaQuery.of(context).size;
    ScreenUtil.instance = ScreenUtil(width: size.width, height: size.height)
      ..init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          // 更新密码title
          YXLocalizations.share().localString('更新密码'),
          style: TextStyle(color: Color(0xFF00182d), fontSize: 16),
        ),
        backgroundColor: Colors.white,
        leading: BackButton(
          // 返回按钮
          color: Color(0xFF2097ff),
          onPressed: () {
            pageBack();
          },
        ),
        elevation: 0,
      ),
      body: Container(
        color: Colors.white,
        width: ScreenUtil.screenWidth,
        padding: EdgeInsets.only(top: 22),
        // margin: const EdgeInsets.only(top: 22),
        child: ChangeNotifierProvider(
          create: (context) => UpdatePasswordProvide(),
          child: ViewSubviews(),
        ),
      ),
    );
  }
}

// 正在写UI的地方
class ViewSubviews extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.max,
      children: [
        ChangeNotifierProvider(
          create: (context) => udpatePaswordTextFieldModel(),
          child: udpatePaswordTextField(
              controller: Provider.of<UpdatePasswordProvide>(context)
                  .oldPasswordController,
              placeholder: "输入老密码"),
        ),
        ChangeNotifierProvider(
          create: (context) => udpatePaswordTextFieldModel(),
          child: udpatePaswordTextField(
              controller: Provider.of<UpdatePasswordProvide>(context)
                  .newPasswordController,
              placeholder: "输入新密码"),
        ),
        ChangeNotifierProvider(
          create: (context) => udpatePaswordTextFieldModel(),
          child: udpatePaswordTextField(
              controller: Provider.of<UpdatePasswordProvide>(context)
                  .ensurePasswordController,
              placeholder: "确认新密码"),
        ),
        EnsureButton(),
        Consumer<UpdatePasswordProvide>(
          builder: (context, value, child) {
            // 走到这里，肯定是成功的
            // if (value.responseModel != null) {
            //   pageBack();
            // }
            return Container(
              width: 0.0,
              height: 0.0,
            );
          },
        ),
      ],
    );
  }
}

// 确定按钮
class EnsureButton extends StatelessWidget {
  const EnsureButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UpdatePasswordProvide provide = Provider.of<UpdatePasswordProvide>(context);
    return Container(
      child: Container(
        height: 46,
        width: ScreenUtil.screenWidth - 87,
        margin: EdgeInsets.only(left: 37, right: 37, top: 31),
        child: FlatButton(
          color: Colors.blue,
          splashColor: Colors.transparent,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(23)),
          child: Text(YXLocalizations.share().localString("确定"),
              style: TextStyle(
                color: Colors.white,
                fontSize: 18,
              )),
          onPressed: () {
            print('点击了确定按钮');
            FocusScope.of(context).requestFocus(FocusScopeNode());
            // 调取VM中改变方法
            provide.changePassword();
          },
        ),
      ),
    );
  }
}
